package UI;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class RegisterJFrame extends JFrame implements ActionListener {
    // 注册窗口组件
    private JLabel userFrame = new JLabel(new ImageIcon("image\\UserName.jpg"));
    private JLabel passwordFrame = new JLabel(new ImageIcon("image\\Password.jpg"));
    private JLabel confirmFrame = new JLabel(new ImageIcon("image\\Password.jpg"));
    private JTextField userField = new JTextField();
    private JTextField passwordField = new JTextField();
    private JTextField confirmField = new JTextField();
    private JButton registerBtn = new JButton(new ImageIcon("image\\register.jpg"));
    private MatteBorder border = new MatteBorder(2, 0, 2, 2, new Color(0, 0, 0));

    public RegisterJFrame() {
        initFrame();
        initBackground();
    }

    private void initFrame() {
        // 设置组件位置和样式
        userFrame.setBounds(150, 100, 480, 60);
        passwordFrame.setBounds(150, 200, 480, 60);
        confirmFrame.setBounds(150, 300, 480, 60);

        userField.setBounds(220, 100, 420, 60);
        userField.setFont(new Font("微雅软黑", Font.BOLD, 28));
        userField.setBorder(border);

        passwordField.setBounds(220, 200, 420, 60);
        passwordField.setFont(new Font("微雅软黑", Font.BOLD, 28));
        passwordField.setBorder(border);

        confirmField.setBounds(220, 300, 420, 60);
        confirmField.setFont(new Font("微雅软黑", Font.BOLD, 28));
        confirmField.setBorder(border);

        registerBtn.setBounds(300, 400, 200, 80);
        registerBtn.addActionListener(this);

        // 添加组件
        this.getContentPane().add(userFrame);
        this.getContentPane().add(passwordFrame);
        this.getContentPane().add(confirmFrame);
        this.getContentPane().add(userField);
        this.getContentPane().add(passwordField);
        this.getContentPane().add(confirmField);
        this.getContentPane().add(registerBtn);

        // 窗口设置
        this.setTitle("拼图游戏——注册");
        this.setSize(750, 750);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setVisible(true);
    }

    private void initBackground() {
        JLabel bgLabel = new JLabel(new ImageIcon("image\\RegisterBackground.jpg"));
        bgLabel.setBounds(0, 0, 750, 750);
        this.getContentPane().add(bgLabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == registerBtn) {
            String username = userField.getText();
            String password = passwordField.getText();
            String confirm = confirmField.getText();

            if (username.isEmpty() || password.isEmpty()) {
                showError("用户名或密码不能为空");
                return;
            }

            if (!password.equals(confirm)) {
                showError("两次密码不一致");
                return;
            }

            if (LoginJFrame.compare("Users.txt", username)) {
                showError("用户名已存在");
                return;
            }

            try {
                // 追加写入用户信息
                BufferedWriter userWriter = new BufferedWriter(new FileWriter("Users.txt", true));
                userWriter.write(username + "\n");
                userWriter.close();

                BufferedWriter pwdWriter = new BufferedWriter(new FileWriter("password.txt", true));
                pwdWriter.write(password + "\n");
                pwdWriter.close();

                JOptionPane.showMessageDialog(this, "注册成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                this.dispose(); // 关闭注册窗口
                new LoginJFrame(); // 返回登录界面
            } catch (IOException ex) {
                ex.printStackTrace();
                showError("文件保存失败");
            }
        }
    }

    private void showError(String message) {
        JOptionPane.showMessageDialog(this, message, "错误", JOptionPane.ERROR_MESSAGE);
    }
}